'use strict';

/**
 * 登陆
 * Created by zhaijg.
 */
app.controller('SigninFormController', ['$scope', '$state', '$rootScope', '$cookieStore', 'adminServices', 'token', 'toolsService','$timeout','$window',
    function ($scope, $state, $rootScope, $cookieStore, adminServices, token, toolsService,$timeout,$window) {
        $scope.user = {
            "account": "", "pws": ""
        };
        if ($cookieStore.get("lingjin_account") != undefined) {
            $scope.user.account = $cookieStore.get("lingjin_account");
        }
        $scope.authError = null;
        app.token = token;
        app.isErrorSend = true;
        $scope.login = function () {
            $scope.authError = '';
            adminServices.adminLogin($scope.user).success(function (resultData) {
                if (resultData.code == "0") {
                    $rootScope.user = resultData.result;
                    app.token = $rootScope.user.token;
                    $cookieStore.put("lingjin_user", $rootScope.user);
                    $cookieStore.put("lingjin_account", $rootScope.user.account);
                    var goto = $rootScope.initData();
                    $state.go(goto);
                }
                else if (resultData.code == "41") {
                    $scope.authError = '账户已关闭';
                }
                else if (resultData.code == "5002") {
                    $scope.authError = '验证失败';
                }
                else {
                    $scope.authError = '账户密码错误';
                }
            }, function (x) {
                $scope.authError = 'Server Error';

            });

        };
    }]);